/**
 * 
 */
package it.agm.fanta.fragment;

import it.agm.activity.dialog.GenericDialog;
import it.agm.fanta.FantaserviceApp;
import it.agm.fanta.R;
import it.agm.fanta.asynctask.jersey.patch.NetworkHandler;
import it.agm.fanta.asynctask.util.SyncUtility;
import it.agm.fanta.exception.ConnectionException;
import android.support.v4.app.Fragment;

/**
 * @author alberto
 * 
 */
public class FragmentSessionFactory {

	private static final String LEGA_NON_ABILITATA = "Lega non abilitata";

	/**
	 * Metodo che si occupa di effettuare lo switch sul testo del menu e
	 * recuperare il fragment da associare
	 * 
	 * @param textMenuItem
	 *            test della voce del NavigationDrawer a cui associare il
	 *            Fragment
	 * @return Fragment da visualizzare
	 * @throws ConnectionException
	 */
	public static Fragment getFragmentById(String textMenuItem)
			throws ConnectionException {

		Fragment fragment = null;
		if(FantaserviceApp.getAppContext().getResources().getText(R.string.action_classifica).equals(textMenuItem)){
			if (FantaserviceApp.classificaFragment==null){
				FantaserviceApp.classificaFragment = new ClassificaFragment();
			}
			fragment = FantaserviceApp.classificaFragment;
		}else if(FantaserviceApp.getAppContext().getResources().getText(R.string.action_calendario).equals(textMenuItem)){
			NetworkHandler networkHandler = NetworkHandler.getInstance();
			SyncUtility.syncCalendario(networkHandler);
			if (FantaserviceApp.calendarioFragment==null){
				FantaserviceApp.calendarioFragment = new CalendarioFragment();
			}
			FantaserviceApp.request.setCoppa(false);
			SyncUtility.syncCalendario(NetworkHandler.getInstance());
			fragment = FantaserviceApp.calendarioFragment;
		}else if(FantaserviceApp.getAppContext().getResources().getText(R.string.action_classifica_marcatori).equals(textMenuItem)){
			if (FantaserviceApp.classificaMarcatoriFragment==null){
				FantaserviceApp.classificaMarcatoriFragment = new ClassificaMarcatoriFragment();
			}
			fragment = FantaserviceApp.classificaMarcatoriFragment;
		}else if(FantaserviceApp.getAppContext().getResources().getText(R.string.action_rosa).equals(textMenuItem)){
			if (FantaserviceApp.rosaFragment==null){
				FantaserviceApp.rosaFragment = new RosaFragment();
			}
			fragment = FantaserviceApp.rosaFragment;
		}else if(FantaserviceApp.getAppContext().getResources().getText(R.string.action_formazione_read).equals(textMenuItem)){
			if (FantaserviceApp.formazioneReadFragment==null){
				FantaserviceApp.formazioneReadFragment = new FormazioneReadFragment();
			}
			FantaserviceApp.formazioneReadFragment.caricaListLega();
			fragment = FantaserviceApp.formazioneReadFragment;
		}else if(FantaserviceApp.getAppContext().getResources().getText(R.string.action_formazione_coppa).equals(textMenuItem)){
			if(FantaserviceApp.lega.isCoppa()){
				if (FantaserviceApp.formazioneReadFragment==null){
					FantaserviceApp.formazioneReadFragment = new FormazioneReadFragment();
				}
				FantaserviceApp.formazioneReadFragment.caricaListaCoppa();
				fragment = FantaserviceApp.formazioneReadFragment;
			}else{
				new GenericDialog(FantaserviceApp.currentActivity(), LEGA_NON_ABILITATA,"ERROR");
				fragment = new HomeFragment();
			}
		}else if(FantaserviceApp.getAppContext().getResources().getText(R.string.action_calendario_coppa).equals(textMenuItem)){
			if(FantaserviceApp.lega.isCoppa()){
				NetworkHandler networkHandler = NetworkHandler.getInstance();
				SyncUtility.syncCalendarioCoppa(networkHandler);
				if (FantaserviceApp.calendarioCoppaFragment==null){
					FantaserviceApp.calendarioCoppaFragment = new CalendarioCoppaFragment();
				}
				FantaserviceApp.request.setCoppa(true);
				SyncUtility.syncCalendarioCoppa(NetworkHandler.getInstance());
				
				fragment = FantaserviceApp.calendarioCoppaFragment;
			}else{
				new GenericDialog(FantaserviceApp.currentActivity(), LEGA_NON_ABILITATA,"ERROR");
				fragment = new HomeFragment();
			}
		}else if(FantaserviceApp.getAppContext().getResources().getText(R.string.action_classifica_coppa).equals(textMenuItem)){
			if(FantaserviceApp.lega.isCoppa()){
				if (FantaserviceApp.classificaCoppaFragment==null){
					FantaserviceApp.classificaCoppaFragment = new ClassificaCoppaFragment();
				}
				fragment = FantaserviceApp.classificaCoppaFragment;
			}else{
				new GenericDialog(FantaserviceApp.currentActivity(), LEGA_NON_ABILITATA,"ERROR");
				fragment = new HomeFragment();
			}	
		}else  if(FantaserviceApp.getAppContext().getResources().getText(R.string.action_live).equals(textMenuItem)){
			
			fragment = new HomeFragment();
		}else{
			fragment = new HomeFragment();
		}	

		return fragment;
	}
}
